%CMFXYZ	Color matching function
%
% The color matching function is the XYZ tristimulus required to match a 
% particular wavelength excitation.
%
% XYZ = CMFXYZ(LAMBDA) is the CIE XYZ color matching function (Nx3) for illumination
% at wavelength LAMBDA (Nx1) [m].  If LAMBDA is a vector then each row of XYZ
% is the color matching function of the corresponding element of LAMBDA. 
%
% XYZ = CMFXYZ(LAMBDA, E) is the CIE XYZ color matching (1x3) function for an 
% illumination spectrum E (Nx1) defined at corresponding wavelengths
% LAMBDA (Nx1).
%
% Note::
% - CIE 1931 2-deg XYZ CMFs from cvrl.ioo.ucl.ac.uk
%
% References::
%  - Robotics, Vision & Control, Section 14.3,
%    P. Corke, Springer 2011.
%
% See also CMFRGB, CCXYZ.



% Copyright (C) 1993-2011, by Peter I. Corke
%
% This file is part of The Machine Vision Toolbox for Matlab (MVTB).
% 
% MVTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% MVTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU Lesser General Public License for more details.
% 
% You should have received a copy of the GNU Leser General Public License
% along with MVTB.  If not, see <http://www.gnu.org/licenses/>.


function XYZ = cmfxyz(lambda, spect)
    if true
        ciedat = [
        0	0	0	0
    % CIE 1931 2-deg XYZ CMFs from cvrl.ioo.ucl.ac.uk
    360,0.000129900000,0.000003917000,0.000606100000
    365,0.000232100000,0.000006965000,0.001086000000
    370,0.000414900000,0.000012390000,0.001946000000
    375,0.000741600000,0.000022020000,0.003486000000
    380,0.001368000000,0.000039000000,0.006450001000
    385,0.002236000000,0.000064000000,0.010549990000
    390,0.004243000000,0.000120000000,0.020050010000
    395,0.007650000000,0.000217000000,0.036210000000
    400,0.014310000000,0.000396000000,0.067850010000
    405,0.023190000000,0.000640000000,0.110200000000
    410,0.043510000000,0.001210000000,0.207400000000
    415,0.077630000000,0.002180000000,0.371300000000
    420,0.134380000000,0.004000000000,0.645600000000
    425,0.214770000000,0.007300000000,1.039050100000
    430,0.283900000000,0.011600000000,1.385600000000
    435,0.328500000000,0.016840000000,1.622960000000
    440,0.348280000000,0.023000000000,1.747060000000
    445,0.348060000000,0.029800000000,1.782600000000
    450,0.336200000000,0.038000000000,1.772110000000
    455,0.318700000000,0.048000000000,1.744100000000
    460,0.290800000000,0.060000000000,1.669200000000
    465,0.251100000000,0.073900000000,1.528100000000
    470,0.195360000000,0.090980000000,1.287640000000
    475,0.142100000000,0.112600000000,1.041900000000
    480,0.095640000000,0.139020000000,0.812950100000
    485,0.057950010000,0.169300000000,0.616200000000
    490,0.032010000000,0.208020000000,0.465180000000
    495,0.014700000000,0.258600000000,0.353300000000
    500,0.004900000000,0.323000000000,0.272000000000
    505,0.002400000000,0.407300000000,0.212300000000
    510,0.009300000000,0.503000000000,0.158200000000
    515,0.029100000000,0.608200000000,0.111700000000
    520,0.063270000000,0.710000000000,0.078249990000
    525,0.109600000000,0.793200000000,0.057250010000
    530,0.165500000000,0.862000000000,0.042160000000
    535,0.225749900000,0.914850100000,0.029840000000
    540,0.290400000000,0.954000000000,0.020300000000
    545,0.359700000000,0.980300000000,0.013400000000
    550,0.433449900000,0.994950100000,0.008749999000
    555,0.512050100000,1.000000000000,0.005749999000
    560,0.594500000000,0.995000000000,0.003900000000
    565,0.678400000000,0.978600000000,0.002749999000
    570,0.762100000000,0.952000000000,0.002100000000
    575,0.842500000000,0.915400000000,0.001800000000
    580,0.916300000000,0.870000000000,0.001650001000
    585,0.978600000000,0.816300000000,0.001400000000
    590,1.026300000000,0.757000000000,0.001100000000
    595,1.056700000000,0.694900000000,0.001000000000
    600,1.062200000000,0.631000000000,0.000800000000
    605,1.045600000000,0.566800000000,0.000600000000
    610,1.002600000000,0.503000000000,0.000340000000
    615,0.938400000000,0.441200000000,0.000240000000
    620,0.854449900000,0.381000000000,0.000190000000
    625,0.751400000000,0.321000000000,0.000100000000
    630,0.642400000000,0.265000000000,0.000049999990
    635,0.541900000000,0.217000000000,0.000030000000
    640,0.447900000000,0.175000000000,0.000020000000
    645,0.360800000000,0.138200000000,0.000010000000
    650,0.283500000000,0.107000000000,0.000000000000
    655,0.218700000000,0.081600000000,0.000000000000
    660,0.164900000000,0.061000000000,0.000000000000
    665,0.121200000000,0.044580000000,0.000000000000
    670,0.087400000000,0.032000000000,0.000000000000
    675,0.063600000000,0.023200000000,0.000000000000
    680,0.046770000000,0.017000000000,0.000000000000
    685,0.032900000000,0.011920000000,0.000000000000
    690,0.022700000000,0.008210000000,0.000000000000
    695,0.015840000000,0.005723000000,0.000000000000
    700,0.011359160000,0.004102000000,0.000000000000
    705,0.008110916000,0.002929000000,0.000000000000
    710,0.005790346000,0.002091000000,0.000000000000
    715,0.004109457000,0.001484000000,0.000000000000
    720,0.002899327000,0.001047000000,0.000000000000
    725,0.002049190000,0.000740000000,0.000000000000
    730,0.001439971000,0.000520000000,0.000000000000
    735,0.000999949300,0.000361100000,0.000000000000
    740,0.000690078600,0.000249200000,0.000000000000
    745,0.000476021300,0.000171900000,0.000000000000
    750,0.000332301100,0.000120000000,0.000000000000
    755,0.000234826100,0.000084800000,0.000000000000
    760,0.000166150500,0.000060000000,0.000000000000
    765,0.000117413000,0.000042400000,0.000000000000
    770,0.000083075270,0.000030000000,0.000000000000
    775,0.000058706520,0.000021200000,0.000000000000
    780,0.000041509940,0.000014990000,0.000000000000
    785,0.000029353260,0.000010600000,0.000000000000
    790,0.000020673830,0.000007465700,0.000000000000
    795,0.000014559770,0.000005257800,0.000000000000
    800,0.000010253980,0.000003702900,0.000000000000
    805,0.000007221456,0.000002607800,0.000000000000
    810,0.000005085868,0.000001836600,0.000000000000
    815,0.000003581652,0.000001293400,0.000000000000
    820,0.000002522525,0.000000910930,0.000000000000
    825,0.000001776509,0.000000641530,0.000000000000
    830,0.000001251141,0.000000451810,0.000000000000
        1e9	0	0	0];
    else
        ciedat = [
            0 0 0 0
    360.000000000000,0.000000122200,0.000000013398,0.000000535027
    365.000000000000,0.000000919270,0.000000100650,0.000004028300
    370.000000000000,0.000005958600,0.000000651100,0.000026143700
    375.000000000000,0.000033266000,0.000003625000,0.000146220000
    380.000000000000,0.000159952000,0.000017364000,0.000704776000
    385.000000000000,0.000662440000,0.000071560000,0.002927800000
    390.000000000000,0.002361600000,0.000253400000,0.010482200000
    395.000000000000,0.007242300000,0.000768500000,0.032344000000
    400.000000000000,0.019109700000,0.002004400000,0.086010900000
    405.000000000000,0.043400000000,0.004509000000,0.197120000000
    410.000000000000,0.084736000000,0.008756000000,0.389366000000
    415.000000000000,0.140638000000,0.014456000000,0.656760000000
    420.000000000000,0.204492000000,0.021391000000,0.972542000000
    425.000000000000,0.264737000000,0.029497000000,1.282500000000
    430.000000000000,0.314679000000,0.038676000000,1.553480000000
    435.000000000000,0.357719000000,0.049602000000,1.798500000000
    440.000000000000,0.383734000000,0.062077000000,1.967280000000
    445.000000000000,0.386726000000,0.074704000000,2.027300000000
    450.000000000000,0.370702000000,0.089456000000,1.994800000000
    455.000000000000,0.342957000000,0.106256000000,1.900700000000
    460.000000000000,0.302273000000,0.128201000000,1.745370000000
    465.000000000000,0.254085000000,0.152761000000,1.554900000000
    470.000000000000,0.195618000000,0.185190000000,1.317560000000
    475.000000000000,0.132349000000,0.219940000000,1.030200000000
    480.000000000000,0.080507000000,0.253589000000,0.772125000000
    485.000000000000,0.041072000000,0.297665000000,0.570060000000
    490.000000000000,0.016172000000,0.339133000000,0.415254000000
    495.000000000000,0.005132000000,0.395379000000,0.302356000000
    500.000000000000,0.003816000000,0.460777000000,0.218502000000
    505.000000000000,0.015444000000,0.531360000000,0.159249000000
    510.000000000000,0.037465000000,0.606741000000,0.112044000000
    515.000000000000,0.071358000000,0.685660000000,0.082248000000
    520.000000000000,0.117749000000,0.761757000000,0.060709000000
    525.000000000000,0.172953000000,0.823330000000,0.043050000000
    530.000000000000,0.236491000000,0.875211000000,0.030451000000
    535.000000000000,0.304213000000,0.923810000000,0.020584000000
    540.000000000000,0.376772000000,0.961988000000,0.013676000000
    545.000000000000,0.451584000000,0.982200000000,0.007918000000
    550.000000000000,0.529826000000,0.991761000000,0.003988000000
    555.000000000000,0.616053000000,0.999110000000,0.001091000000
    560.000000000000,0.705224000000,0.997340000000,0.000000000000
    565.000000000000,0.793832000000,0.982380000000,0.000000000000
    570.000000000000,0.878655000000,0.955552000000,0.000000000000
    575.000000000000,0.951162000000,0.915175000000,0.000000000000
    580.000000000000,1.014160000000,0.868934000000,0.000000000000
    585.000000000000,1.074300000000,0.825623000000,0.000000000000
    590.000000000000,1.118520000000,0.777405000000,0.000000000000
    595.000000000000,1.134300000000,0.720353000000,0.000000000000
    600.000000000000,1.123990000000,0.658341000000,0.000000000000
    605.000000000000,1.089100000000,0.593878000000,0.000000000000
    610.000000000000,1.030480000000,0.527963000000,0.000000000000
    615.000000000000,0.950740000000,0.461834000000,0.000000000000
    620.000000000000,0.856297000000,0.398057000000,0.000000000000
    625.000000000000,0.754930000000,0.339554000000,0.000000000000
    630.000000000000,0.647467000000,0.283493000000,0.000000000000
    635.000000000000,0.535110000000,0.228254000000,0.000000000000
    640.000000000000,0.431567000000,0.179828000000,0.000000000000
    645.000000000000,0.343690000000,0.140211000000,0.000000000000
    650.000000000000,0.268329000000,0.107633000000,0.000000000000
    655.000000000000,0.204300000000,0.081187000000,0.000000000000
    660.000000000000,0.152568000000,0.060281000000,0.000000000000
    665.000000000000,0.112210000000,0.044096000000,0.000000000000
    670.000000000000,0.081260600000,0.031800400000,0.000000000000
    675.000000000000,0.057930000000,0.022601700000,0.000000000000
    680.000000000000,0.040850800000,0.015905100000,0.000000000000
    685.000000000000,0.028623000000,0.011130300000,0.000000000000
    690.000000000000,0.019941300000,0.007748800000,0.000000000000
    695.000000000000,0.013842000000,0.005375100000,0.000000000000
    700.000000000000,0.009576880000,0.003717740000,0.000000000000
    705.000000000000,0.006605200000,0.002564560000,0.000000000000
    710.000000000000,0.004552630000,0.001768470000,0.000000000000
    715.000000000000,0.003144700000,0.001222390000,0.000000000000
    720.000000000000,0.002174960000,0.000846190000,0.000000000000
    725.000000000000,0.001505700000,0.000586440000,0.000000000000
    730.000000000000,0.001044760000,0.000407410000,0.000000000000
    735.000000000000,0.000727450000,0.000284041000,0.000000000000
    740.000000000000,0.000508258000,0.000198730000,0.000000000000
    745.000000000000,0.000356380000,0.000139550000,0.000000000000
    750.000000000000,0.000250969000,0.000098428000,0.000000000000
    755.000000000000,0.000177730000,0.000069819000,0.000000000000
    760.000000000000,0.000126390000,0.000049737000,0.000000000000
    765.000000000000,0.000090151000,0.000035540500,0.000000000000
    770.000000000000,0.000064525800,0.000025486000,0.000000000000
    775.000000000000,0.000046339000,0.000018338400,0.000000000000
    780.000000000000,0.000033411700,0.000013249000,0.000000000000
    785.000000000000,0.000024209000,0.000009619600,0.000000000000
    790.000000000000,0.000017611500,0.000007012800,0.000000000000
    795.000000000000,0.000012855000,0.000005129800,0.000000000000
    800.000000000000,0.000009413630,0.000003764730,0.000000000000
    805.000000000000,0.000006913000,0.000002770810,0.000000000000
    810.000000000000,0.000005093470,0.000002046130,0.000000000000
    815.000000000000,0.000003767100,0.000001516770,0.000000000000
    820.000000000000,0.000002795310,0.000001128090,0.000000000000
    825.000000000000,0.000002082000,0.000000842160,0.000000000000
    830.000000000000,0.000001553140,0.000000629700,0.000000000000
            1e9 0 0 0];
    end

        XYZ = interp1(ciedat(:,1)*1e-9, ciedat(:,2:4), lambda, 'pchip', 0);
        if nargin == 2
            % approximate rectangular integration
            dlambda = lambda(2) - lambda(1);
            XYZ = spect(:)' * XYZ * dlambda;
        end
